package cn.biplam.front.template.complex;

import cn.biplam.common.paramters.name.NameCommon;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlImage;
import cn.biplam.front.html.HtmlText;
import cn.biplam.front.html.HtmlValue;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Length_Unit;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.template.Template;
import cn.biplam.front.template.base.HiddenParams;

/**
 * 向下展开的区域块<BR>
 * 标题文字，点击后向下展开区域块(Action请求)<BR>
 * 用于降低管理页面首次显示内容，提高加载速度<BR>
 */
public class ExpandBlock implements Template{

	private String cId;
	private String nodeTitle;
	private String actionId;
	private boolean defaultShow;
	private int width;
	private String sHomeUrl;
	private HiddenParams params;

	/**
	 * 构造方法
	 * @param cId				页面容器ID
	 * @param nodeTitle			标题文字
	 * @param contentActionId	区块内容请求ActionId
	 * @param defaultShow		默认是否展开
	 * @param width				页面宽度
	 * @param height			内容块页面高度
	 * @param sHomeUrl			主页URL
	 * @throws ErrorMessage 
	 */
	public ExpandBlock(String cId,String nodeTitle,String contentActionId,
			boolean defaultShow,int width,String sHomeUrl) throws ErrorMessage {
		this.cId=cId;
		this.nodeTitle=nodeTitle;
		this.actionId=contentActionId;
		this.defaultShow=defaultShow;
		this.width=width;
		this.sHomeUrl=sHomeUrl;
		this.params=new HiddenParams(this.cId+"Call","1");
	}

	/**
	 * 创建字段选择列表的表节点
	 */
	HtmlWindow getTitle() 
	throws ErrorMessage{
		HtmlWindow titleDiv = new HtmlDiv(this.cId+"Title","exdTdiv");
		titleDiv.bind(Window_Event.OnmouseOver, "exdOnNode('"+this.cId+"');");
		titleDiv.setStyleWidth(width);

		//图片
		HtmlImage mImage;
		if(this.defaultShow){
			titleDiv.put(new HtmlValue(this.cId+"status","2"));
			mImage=new HtmlImage(sHomeUrl+"/images/spreadup.gif","exdTimg");
		}else{
			titleDiv.put(new HtmlValue(this.cId+"status","1"));
			mImage=new HtmlImage(sHomeUrl+"/images/spreaddown.gif","exdTimg");
		}
		mImage.setID(this.cId+"Image");
		titleDiv.put(mImage);
		//切换图片url
		titleDiv.put(new HtmlValue(this.cId+"Image1s",sHomeUrl+"/images/spreaddown.gif"));
		titleDiv.put(new HtmlValue(this.cId+"Image1u",sHomeUrl+"/images/spreaddownr.gif"));
		titleDiv.put(new HtmlValue(this.cId+"Image2s",sHomeUrl+"/images/spreadup.gif"));
		titleDiv.put(new HtmlValue(this.cId+"Image2u",sHomeUrl+"/images/spreadupr.gif"));

		//文本信息
		HtmlWindow text = new HtmlText(this.cId+"Text","exdTtxt",this.nodeTitle);
		titleDiv.put(text);

		return titleDiv;
	}

	/**
	 * 返回创建好的HtmlWindow对象
	 * @return HtmlWindow
	 * @throws ErrorMessage
	 */
	public HtmlWindow get() throws ErrorMessage {
		/**1.创建总容器*/
		HtmlWindow div = new HtmlDiv(this.cId);

		/**2.创建标题*/
		div.put(this.getTitle());

		/**3.1 创建内容区域*/
		HtmlWindow contentDiv=new HtmlDiv(this.cId+"Content","exdC");
		contentDiv.setStyleWidth(width);
		if(!this.defaultShow){
			contentDiv.setStyle("display","none");
		}
		/**3.2 设置高度*/
		contentDiv.setStyleHeight("100", Length_Unit.percent);
		div.put(contentDiv);

		/**4.创建提交参数容器*/
		this.params.put(NameCommon.Src_Container_ID,this.params.getID());
		div.put(this.params.get());

		/**5.请求action*/
		div.put(
				new HtmlValue(
						this.cId+"Act",
						AjxScript.getMs(params.getID(), this.actionId, "1", this.cId+"Content")
				)
		);
		
		/**6.立即请求*/
		if(this.defaultShow){
			contentDiv.bind("exdClick('"+this.cId+"');");
		}

		return div;
	}
	
	/**
	 * 放入一个需要提交给请求组件的数据参数
	 */
	public void putParam(String key, String value) throws ErrorMessage {
		this.params.put(key, value);
	}

}
